/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Communication.MyResultSet;
import GUI.Multilang.MyResources;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author TrongKhoa
 */
public class MyQueryPanel extends JPanel implements ActionListener
{

    frmClient _parent;
    GridBagConstraints constraints = new GridBagConstraints();
    MyAutoCompleteTextArea _txtQuery;
    JTabbedPane _tabResult;
    JPanel _panelResult;
    JPanel _panelMessage;
    JTextArea _textAreaMessage;

    public MyQueryPanel(frmClient parent)
    {
        super();
        _parent = parent;
        InitComponents();
    }
    private String[] SQLKeywords = new String[]
    {
        "select", "from", "where", "group", "by", "having", "order",
        "delete", "insert", "update", "set", "drop", "create", "primary", "key", "table",
        "null", "not", "null", "null", "foreign", "references", "distinct", "asc", "desc",
        "on", "values", "into", "like",
        "int", "float", "nvarchar", "date",
        "or", "and",
        "count(", "sum(", "max(", "min(",
    };

    private void AddComponent(Component cpn, int row, int col, int width, int height, double weightx, double weighty)
    {
        //fill
        //ipadx, ipady (internal)
        //insets: external
        //anchor: center, pagestart, pageend, linestart, vv
        //weightx, weighty: ti le co dan
        constraints.gridy = row;
        constraints.gridx = col;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        constraints.weightx = weightx;
        constraints.weighty = weighty;

        this.add(cpn, constraints);
    }

    private void InitComponents()
    {
        this.setLayout(new GridBagLayout());
        _txtQuery = new MyAutoCompleteTextArea();
        ResetVocabularyForAutoCompleteFunction();

        JScrollPane scroll = new JScrollPane(_txtQuery);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(12, 12, 12, 12);
        AddComponent(scroll, 0, 0, 1, 1, 0.5, 0.3);

        _tabResult = new JTabbedPane();
        _panelResult = new JPanel(new BorderLayout());

        JScrollPane x = new JScrollPane(_panelResult);
        x.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        x.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        _tabResult.addTab(MyResources.GetString("result"), x);

        _panelMessage = new JPanel();
        _textAreaMessage = new JTextArea(10, 10);
        _textAreaMessage.setLineWrap(true);
        _panelMessage.setLayout(new BorderLayout());
        _panelMessage.add(_textAreaMessage, BorderLayout.CENTER);
        JScrollPane y = new JScrollPane(_panelMessage);
        y.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        y.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        _tabResult.addTab(MyResources.GetString("message"), y);

        AddComponent(_tabResult, 1, 0, 1, 1, 0.5, 0.7);


        constraints.fill = GridBagConstraints.NONE;
        constraints.insets = new Insets(12, 12, 12, 12);
        JButton btnExec = new JButton(MyResources.GetString("execute"));
        btnExec.setActionCommand("ExecQuery");
        btnExec.addActionListener(this);

        btnExec.setPreferredSize(new Dimension(100, 25));
        btnExec.setMinimumSize(new Dimension(100, 25));
        AddComponent(btnExec, 0, 1, 1, 1, 0, 0);


        JButton btnSave = new JButton(MyResources.GetString("save"));
        btnSave.setActionCommand("SaveQuery");
        btnSave.addActionListener(this);

        btnSave.setMinimumSize(new Dimension(100, 25));
        btnSave.setPreferredSize(new Dimension(100, 25));
        AddComponent(btnSave, 1, 1, 1, 1, 0, 0);
    }

    public void ResetVocabularyForAutoCompleteFunction()
    {
        _txtQuery.ClearVocabulary();
        _txtQuery.AddRangeVocabulary(SQLKeywords);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        switch (e.getActionCommand())
        {
            case "ExecQuery":
                _parent.client.ExecuteQuery(_txtQuery.getText());
                break;
        }

    }

    public void AddMessage(String message)
    {
        _tabResult.setSelectedIndex(1);
        _textAreaMessage.append("\n" + message);
    }

    public void AddResult(MyResultSet result)
    {
        _tabResult.setSelectedIndex(0);
        if (result._IsUpdateResult)
        {
            _panelResult.removeAll();
            _panelResult.add(new JLabel(result._UpdateQueryResult + " row(s) affected!"));
            revalidate();
        }
        else
        {
            _panelResult.removeAll();
            QueryResultTableModel model = new QueryResultTableModel(result._Rows, result._Columns);
            JTable resultTable = new JTable(model);
            _panelResult.add(resultTable.getTableHeader(), BorderLayout.NORTH);
            _panelResult.add(resultTable, BorderLayout.CENTER);
            model.fireTableDataChanged();
        }
    }
}
